package com.shared.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.shared.util.JwtUtils;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class SecurityFilter extends ZuulFilter {
    //过滤器类型
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    //过滤器执行顺序
    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        String uri = request.getRequestURI();
        System.out.println(uri);
        if (uri.endsWith("/userLogin") || uri.endsWith("/sendRegCode") || uri.endsWith("/register") || uri.endsWith("/sendBackCode") || uri.endsWith("/findPwd") || uri.endsWith("/points") || uri.endsWith("/tengxun") || uri.endsWith("/saveStorage") || uri.endsWith("/getStorage") || uri.endsWith("savePosition") || uri.endsWith("getPosition") || uri.startsWith("/order-service") || uri.startsWith("/merchant-service") || uri.startsWith("hystrix-dash-board") || uri.startsWith("shoppingservice") || uri.startsWith("pay-service") || uri.startsWith("/zipkin")) {
            return false;
        }
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        /*
            鉴权：判断用户是否登录
         */
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        String token = request.getHeader("token");
        if (token == null || "".trim().endsWith(token)) {
            //请求不继续向后路由
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
            requestContext.setResponseBody("{\"code\":401,\"message\":\"token not found\"}");
        } else {
            /*
                检查是否过期，篡改
             */
            try {
                JwtUtils.parseToken(token);
                requestContext.setSendZuulResponse(true);
                requestContext.setResponseStatusCode(HttpStatus.OK.value());
            } catch (ExpiredJwtException e) {
                e.printStackTrace();
                requestContext.setSendZuulResponse(false);
                requestContext.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
                requestContext.setResponseBody("{\"code\":403,\"message\":\"token 失效\"}");
            } catch (Exception e) {
                e.printStackTrace();
                requestContext.setSendZuulResponse(false);
                requestContext.setResponseStatusCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
                requestContext.setResponseBody("{\"code\":500,\"message\":\"error\"}");
            }
        }
        return null;
    }
}
